<?php

namespace App\Http\Controllers\Admin;

use App\Models\Orm\Apply;
use App\Models\Orm\Banner;
use App\Models\Orm\Candy;
use App\Models\Orm\Grade;
use App\Models\Orm\Market;
use App\Models\Orm\AwAuditRecord;
use App\Models\Orm\CooperativeCompany;
use App\Models\Orm\Pay;
use App\Models\Orm\Sconfig;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 产品模块控制器
 *
 * Class ProductController
 * @package App\Http\Controllers\Admin
 */
class ApplyController extends Controller
{
    /**
     * 产品列表(展示页)
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = auth('admin')->user();

        $data = Apply::orderby('id', SORT_DESC)->get();
        return view('admin.apply.index', compact('data'));

    }

    public function online($id)
    {


        // 当前用户
        $user = auth('admin')->user();

        // 产品信息
        $product = Apply::find($id);



        if (!$product) {
            return back()->withErrors('未找到产品');
        }

        if ($product->status != Apply::OFF) {
            return back()->withErrors('该产品已经查看');
        }
        $product ->status = 1;

        $product ->save();

        return back()->with(['success' => '您的上线申请已提交,请耐心等待审核结果']);
    }

    /**
     * 产品下线
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function offline($id)
    {
        /**
         * 清除分类主题产品对照表关联数据
         * 清除产品标签表关联数据
         * 清除热门推荐表关联数据
         * 清除秒杀表关联数据
         */
        // 当前用户
        $user = auth('admin')->user();

        // 产品
        $product = Apply::find($id);
        if (!$product) {
            return back()->withErrors('未找到相关广告');
        }


        if ($product->status != Apply::ON) {
            return back()->withErrors('该产品未未查看');
        }

        $product ->status = 0;

        $product ->save();
        return back()->with(['success' => '产品下线成功']);
    }


}